package st.business.tepth.business.driverinfo;

import com.alibaba.fastjson.TypeReference;

import io.reactivex.disposables.Disposable;
import st.business.tepth.base.OnDataConvertListener;
import st.business.tepth.business.bean.DriverWorkRecordBean;
import st.business.tepth.business.bean.PagingBean;
import st.business.tepth.config.Url;
import st.shadow.library.core.net.callback.IError;
import st.shadow.library.core.net.callback.ISuccess;
import st.shadow.library.core.net.data.ObjConverter;
import st.shadow.library.core.net.rx.RxRestClient;

/**
 * Author: shadow
 * Email:  1025431817@qq.com
 * Time:   2019-03-01 11:55
 */

public class DriverInfoModel implements DriverInfoContract.Model {

    @Override
    public Disposable getWorkRecord(String token, String jobNo, String startDate,
                                    String endDate, int pageNumber,
                                    int pageSize,
                                    final OnDataConvertListener<PagingBean<DriverWorkRecordBean>> listener) {
        return RxRestClient.builder()
                .url(Url.GetDriverRepairRecord)
                .params("token", token)
                .params("jobNo", jobNo)
                .params("startDate", startDate)
                .params("endDate", endDate)
                .params("pageNumber", pageNumber)
                .params("pageSize", pageSize)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        listener.convert(convertToInfo(response));
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        listener.error(code, msg);
                    }
                })
                .build()
                .post();
    }

    private PagingBean<DriverWorkRecordBean> convertToInfo(String response) {
        return new ObjConverter<PagingBean<DriverWorkRecordBean>>().convert(response
                , new TypeReference<PagingBean<DriverWorkRecordBean>>() {
                });
    }
}
